import { Component, OnInit } from '@angular/core';
import { NavController } from '@ionic/angular';
import { ActivatedRoute, Params } from '@angular/router';
import * as constants from 'src/app/providers/Constants'
import { Router } from '@angular/router';
import { ShopService } from 'src/app/services/shop.service';
import { ToastService } from 'src/app/providers/toast/toast.service';
import { Storage } from 'src/app/providers/Storage';
import { Utils } from 'src/app/providers/Utils';

@Component({
  selector: 'app-goods-search-list',
  templateUrl: './goods-search-list.page.html',
  styleUrls: ['./goods-search-list.page.scss'],
})
export class GoodsSearchListPage implements OnInit {
  titleStr: string = ''
  itemSelect: number = 0;
  priceOrder: number = 0;
  goodsInfo: any = [];
  imageUrl: string = ''
  categoryDetailData: any = '';
  procSalesVolume: string = '';
  procClassId: string = '';
  procCreateTime: string = '';
  procName: string = '';
  lowPrice: string = '';
  heightPrice: string = '';
  procBrandName: string = ''
  procStandards: string = ''
  type: string = '1'
  procBodyColor: string = ''
  data: any = {
    nowPage: 1,
    pageSize: 100,
    procClassId: '',
    procName: ''//产品名称
  }
  isFast: boolean = false;
  constructor(private nav: NavController, private activeRoute: ActivatedRoute, public service: ShopService, private toast: ToastService) {
    this.activeRoute.queryParams.subscribe((params: Params) => {
      if (params.searchType == 0) {
        let d = JSON.parse(params.goodsInfo);
        console.log(d);
        if (d != undefined) {
          this.titleStr = d.procClassName;
          this.procClassId = d.procClassId;
        }
      } else {
        let d = JSON.parse(params.goodsInfo);
        console.log(d);
        if (d != undefined) {
          this.titleStr = d.searchValue;
          this.procName = d.searchValue;
        }
      }

    });
    this.imageUrl = this.service.getImageUrl();
    Storage.sessionStorage.remove("filtrateData");
    this.getList();
  }

  ionViewDidEnter() {
    let filtrateData = Storage.sessionStorage.get('filtrateData');
    this.type = '1'
    if (filtrateData) {
      this.procBrandName = filtrateData.brandName;
      this.procStandards = filtrateData.measureName;
      this.procBodyColor = filtrateData.colorName;
      this.lowPrice = filtrateData.lowPrice;
      if (filtrateData.procClassId == '') {
      } else {
        this.type = '0'
        this.procClassId = filtrateData.procClassId;
      }
      this.heightPrice = filtrateData.heightPrice;
      if (this.isFast) {
        this.getList();
      }
      this.isFast = true;
    }

  }

  solaImage(img) {
    return Utils.imageUrl(this.imageUrl, img);
  }

  onItemDetail(info) {
    this.nav.navigateForward(['/goods-details'], {
      queryParams: {
        shopInfo: JSON.stringify(info)
      }
    });
  }
  AmountOfConversion(num: number) {
    if (!isNaN(num)) return num / 100;
    return 0;
  }
  getList() {
    this.categoryDetailData = [];
    console.log('imageUrl', this.imageUrl);
    // this.data.procClassId=index
    let order = null;
    if (this.priceOrder != 0) {
      order = this.priceOrder;
    }
    let data = {};
    if (this.lowPrice) {
      data = {
        procClassId: this.procClassId,
        type: this.type,
        procSalesVolume: this.procSalesVolume,
        procCreateTime: this.procCreateTime,
        procBrandName: this.procBrandName,
        procStandards: this.procStandards,
        procBodyColor: this.procBodyColor,
        lowPrice: Number(this.lowPrice) * 100,
        heightPrice: Number(this.heightPrice) * 100,
        procName: this.procName,
        procPrice: order
      }
    } else {
      data = {
        type: this.type,
        procClassId: this.procClassId,
        procSalesVolume: this.procSalesVolume,
        procCreateTime: this.procCreateTime,
        procBrandName: this.procBrandName,
        procStandards: this.procStandards,
        procBodyColor: this.procBodyColor,
        procName: this.procName,
        procPrice: order
      }
    }

    this.service.getProcList(data, { showLoading: true }).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
        res.procArray.forEach(element => {
          element.procImg = element.procImg.split(',')[0];
        });
        this.categoryDetailData = res.procArray;
      } else if (constants.errCode[res.retCode]) {
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });
  }
  item(i) {
    this.procSalesVolume = '';
    this.procCreateTime = '';
    this.itemSelect = i;
    if (i == 3) {
      if (this.priceOrder == 1) {
        this.priceOrder = 2;
      } else {
        this.priceOrder = 1;
      }
      this.getList();
    }
    if (i == 2) {
      this.procSalesVolume = '1';
      this.getList();
    }
    if (i == 1) {
      this.procCreateTime = '1';
      this.getList();
    }
    if (i == 0) {
      this.procSalesVolume = '';
      this.priceOrder = null;
      this.getList();
    }
    if (i == 4) {
      this.nav.navigateForward('goods-filtrate');
    }
  }

  ngOnInit() {
    Storage.sessionStorage.remove('idxList');
    Storage.sessionStorage.remove('filtrateData');
  }

}
